package cn.com.aiton.educenter.service.impl;

import cn.com.aiton.educenter.entity.PestsProject;
import cn.com.aiton.educenter.entity.UcenterMember;
import cn.com.aiton.educenter.entity.UcenterProjectMember;
import cn.com.aiton.educenter.mapper.PestsProjectMapper;
import cn.com.aiton.educenter.service.PestsProjectService;
import cn.com.aiton.educenter.service.UcenterMemberService;
import cn.com.aiton.educenter.service.UcenterProjectMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 防治项目 服务实现类
 * </p>
 *
 * @author testAuthor
 * @since 2021-01-17
 */
@Service
public class PestsProjectServiceImpl extends ServiceImpl<PestsProjectMapper, PestsProject> implements PestsProjectService {
    @Autowired
    UcenterProjectMemberService ucenterProjectMemberService;
    @Autowired
    UcenterMemberService memberService;
    @Override
    public PestsProject getOneByUserId(String userid) {
        QueryWrapper<PestsProject> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userid);
        wrapper.last("LIMIT 1");
        PestsProject member = baseMapper.selectOne(wrapper);
        return member;

    }

    @Override
    public PestsProject initProject(String userId) {
        UcenterMember member = memberService.getById(userId);
        PestsProject pp = new PestsProject();
        pp.setDeskCount(0);
        pp.setFileCount(0);
        pp.setEntities(0);
        pp.setRecordCount(0);
        pp.setName(member.getMobile()+"的默认项目");
        pp.setUserId(userId);
        int i = baseMapper.insert(pp);
        if (i>0){
            UcenterProjectMember upm = new UcenterProjectMember();
            upm.setMemberId(userId);
            upm.setProjectId(pp.getId());
            ucenterProjectMemberService.save(upm);
        }
        return pp;
    }


}
